<?php
/**
 *
 * @author Strawberry🍓
 * @date 2021-05-26 4:05 下午
 *
 */

namespace app\api\controller;


use app\api\service\JwtService;
use app\common\model\tang\Cost;
use app\common\model\tang\ReleaseRecord;
use app\common\model\tang\ReleaseUser;
use think\App;
use think\Db;

class Papers extends Base
{
    public function login()
    {
        $code = $this->request->param('code');
        $iv = $this->request->param('iv');
        $encryptedData = $this->request->param('encryptedData');
        if (empty($code) || empty($iv) || empty($encryptedData)) {
            return SendError("参数错误");
        }
        $getUserInfo = $this->app->openPlatform->auth->session($code); //返回openid 和 sessionkey
        if (!isset($getUserInfo['session_key']) || empty($getUserInfo['session_key']) ){
            return SendError("code不不正确");
        }
        $decryptedData = $this->app->openPlatform->encryptor->decryptData($getUserInfo['session_key'], $iv, $encryptedData);
        $mobile = $decryptedData['phoneNumber'];
//        $mobile = 15029080416;
        // 小程序获取手机号的代码
        $info = Cost::where(['mobile'=>$mobile,'type'=>0])->where('pid','>',0)->order('id desc')->find();
        if (empty($info)){
            return SendError("你不是施工人员无法登陆");
        }
        $is_login = false ;
        if ($info['date_type'] == 1) {
            $is_login = true ;
        } else {
            if($info['start_time']<=time() && $info['end_time']>=time()){
                $is_login = true ;
            }
        }
        if(!$is_login){
            return SendError("证件未在有效期内");
        }
        $jwtAuth = JwtService::getInstance();
        $token   = $jwtAuth->setOpenid($getUserInfo['openid'])
                    ->setUid($info['id'])
                    ->setMobile($info['mobile'])
                    ->encode()
                    ->getToken();
        return SendSuccess("登录成功", $token);
    }

    public function getPapersUserInfo()
    {

        // 小程序获取手机号的代码
        $info = Cost::with(['merchants','costType'])->where(['mobile'=>$this->mobile,'pay_status'=>1,'type'=>0])->where('pid','>',0)
            ->field('id,add_user,username,mobile,merchant_id,id_card_positive,cost_type_id,id_card_negative,other_certificate,date_type,start_time,end_time,create_time')
            ->find();
        if (empty($info)){
            return SendError("没有该人员信息");
        }
        if ($info['date_type'] ==  1){
            $info['date'] = '永久有效';
        }else{
            if ($info['start_time'] > time()){
                return SendError("没有该人员信息");
            }
            if ($info['end_time'] < time()){
                return SendError("通行证已过期");
            }
            $info['date'] = date('Y.m.d',$info['start_time']).'-'.date('Y.m.d',$info['end_time']);
        }
        $info['id_card_positive']  = $info['id_card_positive']?$this->request->domain(). $info['id_card_positive']:'';
        $info['cost_type_id'] = $info['costType']['cost_name'];
        $info['id_card_negative']  = $info['id_card_negative']?$this->request->domain(). $info['id_card_negative']:'';
        $info['other_certificate'] = $info['other_certificate']?$this->request->domain(). $info['other_certificate']:'';
        $info['qrcode'] = 'http://qrcode.kelimx.com/qrcode/code?text='.$info['id'];
        $info['time'] = date('Y/m/d',$info['create_time']);
        return SendSuccess("success", $info);
    }


}